import axios from "axios";
import toast from "@/components/toast";


let BASE_URL = import.meta.env.VITE_BASE_URL;
const NODE_ENV = process.env.NODE_ENV;
const mode = import.meta.env.MODE;
console.log(NODE_ENV, mode, BASE_URL);

//npm run dev[test|prod]时, NODE_ENV 默认是 development,  
//npm run prod时, NODE_ENV 是 production,  
// if (NODE_ENV == 'development') {
//     if (mode == 'dev') {
//         BASE_URL = "devProxy"
//     }
//     if (mode == 'prod') {
//         BASE_URL = "prodProxy"
//     }
// }
console.log("BASE_URL: ", BASE_URL);
//创建axios实例
const service = axios.create({
    baseURL: `${BASE_URL}/`,
    timeout: 10000,
    headers: {
        "Content-type": "application/json;charset=utf-8",
    }
})
//请求拦截
service.interceptors.request.use((config) => {
    config.headers = config.headers || {}
    // if (config.url != '/api/auth/login') {
    //     let access_token = Cookies.get("access_token");
    //     if (access_token) {
    //         config.headers.Authorization = access_token;
    //     }
    // }

    return config;
}, error => {
    return Promise.reject(error)
})
//响应拦截
service.interceptors.response.use((res) => {
    const status = res.status//code是后端的状态码
    // console.log(res);
    if (status == 200) {
        return Promise.resolve(res.data);

    } else {
        return Promise.reject(res)
    }
}, (err) => {
    console.log(err);
    if (err && err.code == "ECONNABORTED" && err.message.indexOf("timeout") != -1) {
        toast(`请求地址:${err.config.baseURL}${err.config.url}<br>请求超时！请您稍后重试`, 2, 5000)
    }
})

export default service
